<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Member.php
 * Created on : 2023-2-28 10:09:02
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\controller;

use app\Request;
use haoteam\service\UtilService;
use haoteam\http\validates\AdminUserValidate;
use haoteam\http\validates\AdminUserRoleValidate;
use app\admin\model\AdminUser;
use app\admin\model\AdminUserNode;
use app\admin\model\AdminUserRole;
use app\admin\model\AdminUserRoleNode;

/**
 * 系统成员管理
 * Class Member
 * Tiger <1192851302@qq.com> 2023-2-28 10:09:02
 */
class Member {

    /**
     * 系统成员管理
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function index(Request $request) {
        $post = UtilService::postMore([
                    'search',
                    'roles'
        ]);
        return app('json')->successful(AdminUser::getList($post));
    }

    /**
     * 添加系统成员
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function create(Request $request) {
        $post = UtilService::postMore([
                    'username',
                    ['password', 123456],
                    ['repassword', 123456],
                    'real_name',
                    'mobile',
                    'email',
                    'roles',
                    'state'
        ]);
        $validate = new AdminUserValidate();
        if ($validate->scene('create')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }

        $post['salt'] = create_salt();
        $post['password'] = encrypt_password($post['password'], $post['salt']);
        if ($request->isSuper() == false) {
            $post['superior'] = $request->adminId();
        }

        try {
            AdminUser::create($post);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('创建成功');
    }

    /**
     * 更新系统成员
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function update(Request $request) {
        $post = UtilService::postMore([
                    'id',
                    'username',
                    'real_name',
                    'mobile',
                    'email',
                    'roles',
                    'state'
        ]);
        $validate = new AdminUserValidate();
        if ($validate->scene('update')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        $where['id'] = $post['id'];
        if ($request->isSuper() == false) {
            $where['superior'] = $request->adminId();
        }
        try {
            AdminUser::update($post, $where);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('更新成功');
    }

    /**
     * 删除系统成员
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function delete(Request $request) {
        [$id] = UtilService::postMore(['id'], $request, true);
        $validate = new AdminUserValidate();
        if ($validate->scene('delete')->check(compact('id')) == false) {
            return app('json')->fail($validate->getError());
        }
        $where['id'] = $id;
        if ($request->isSuper() == false) {
            $where['superior'] = $request->adminId();
        }
        try {
            AdminUser::update(['is_del' => 1], $where);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('删除成功');
    }

    /**
     * 成员权限
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    当权限赋予每个人的时候有效
     * @return type
     */
    public function auth(Request $request) {
        [$id] = UtilService::getMore(['id'], $request, true);
        $validate = new AdminUserValidate();
        if ($validate->scene('delete')->check(compact('id')) == false) {
            return app('json')->fail($validate->getError());
        }
        return app('json')->successful(AdminUserNode::getTree($id));
    }

    /**
     * 成员权限写入
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    当权限赋予每个人的时候有效
     * @return type
     */
    public function set_auth(Request $request) {
        [$id, $nodes] = UtilService::postMore(['id', ['nodes', []]], $request, true);
        $validate = new AdminUserValidate();
        if ($validate->scene('delete')->check(compact('id')) == false) {
            return app('json')->fail($validate->getError());
        }

        try {
            AdminUserNode::createData($id, $nodes);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('保存成功');
    }

    /**
     * 系统角色管理
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @desc    当权限赋予某一个角色组时有效
     * @return type
     */
    public function role() {
        return app('json')->successful(AdminUserRole::getList());
    }

    /**
     * 添加系统角色
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    当权限赋予某一个角色组时有效
     * @return type
     */
    public function role_create() {
        $post = UtilService::postMore([
                    'title',
                    'message'
        ]);
        $validate = new AdminUserRoleValidate();
        if ($validate->scene('create')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            AdminUserRole::create($post);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('添加成功');
    }

    /**
     * 更新系统角色
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    当权限赋予某一个角色组时有效
     * @return type
     */
    public function role_update() {
        $post = UtilService::postMore([
                    'id',
                    'title',
                    'message'
        ]);
        $validate = new AdminUserRoleValidate();
        if ($validate->scene('update')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            AdminUserRole::update($post, ['id' => $post['id']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('更新成功');
    }

    /**
     * 删除系统角色
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    当权限赋予某一个角色组时有效
     * @return type
     */
    public function role_delete(Request $request) {
        [$id] = UtilService::postMore(['id'], $request, true);
        $validate = new AdminUserRoleValidate();
        if ($validate->scene('delete')->check(compact('id')) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            AdminUserRole::update(['is_del' => 1], ['id' => $id]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('删除成功');
    }

    /**
     * 角色权限
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    当权限赋予某一个角色组时有效
     * @return type
     */
    public function role_auth(Request $request) {
        [$id] = UtilService::getMore(['id'], $request, true);
        $validate = new AdminUserRoleValidate();
        if ($validate->scene('delete')->check(compact('id')) == false) {
            return app('json')->fail($validate->getError());
        }
        return app('json')->successful(AdminUserRoleNode::getTree($id));
    }

    /**
     * 角色权限写入
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    当权限赋予某一个角色组时有效
     * @return type
     */
    public function role_write_auth(Request $request) {
        [$id, $nodes] = UtilService::postMore(['id', ['nodes', []]], $request, true);
        $validate = new AdminUserRoleValidate();
        if ($validate->scene('delete')->check(compact('id')) == false) {
            return app('json')->fail($validate->getError());
        }

        try {
            AdminUserRoleNode::createData($id, $nodes);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }

        return app('json')->successful('保存成功');
    }

}
